const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path');

//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

module.exports = function() {
    var router=express.Router();

    router.use('/Addforumcomment',function(req,res,next){
        var forumBName=  req.body.forumBName;
        var time=new Date();
             time=time.toLocaleString();
        if(req.files.length==0)
        {
            var FCcommentPic='timg.jpg';
            db.query('INSERT INTO `forumcomment`(`FCId`, `forumBId`, `userId`, `FCContent`,  `FCcommentPic`, `FCcommentTime`, `FCcommentNum`) VALUES ( ' +
                    '(0,'+parseInt(forumBName)+','+parseInt(req.body.userName)+',"'+req.body.editor+'","'+FCcommentPic+'","'+time
                    +'",0)',function (err,data) {
                    if(err){
                        res.send('<script>alert("程序错误");window.location.href="400.html";</script>').end();
                    }else{
                        if(data.length==0){
                            res.send('<script>alert("添加失败");window.location.href="400.html";</script>').end();
                        }
                        else{
                            res.send('<script>alert("添加成功");window.location.href="forumcomment.html";</script>').end();
                        }
                    }
                });
        }
        else {
            //console.log("原名文件"+req.files[0].originalname);
            //获得文件扩展名
            //console.log("文件后缀"+pathLib.parse(req.files[0].originalname).ext);
            //文件全名
            var ext = pathLib.parse(req.files[0].originalname).ext;
            //console.log("dxcvbnm" + ext);
            //文件旧存放路径
            var oldPath = req.files[0].path;
            // console.log("文件旧存放路径"+oldPath)
            //文件新存放路径
            var newPath = req.files[0].path + ext;
            // console.log("文件新存放路径" + newPath)
            //文件新名
            var newFileName = req.files[0].filename + ext;

            console.log("我上的图片"+newFileName);

            fs.rename(oldPath, newPath, function (err) {
                if (err) {
                    res.status(500).send('upload is error');
                }
                else {

                    db.query('INSERT INTO `forumcomment`(`FCId`, forumBId, `userId`, `FCContent`, `FCcommentPic`, `FCcommentTime`, `FCcommentNum`) VALUES (0,'+parseInt(forumBName)+','+parseInt(req.body.userName)+',"'+req.body.editor+'","'+newFileName+'","'+time+'",0)',function (err,data) {
                        console.log(err);
                        console.log(typeof (parseInt(forumBName)));
                        if(err){
                            res.send('<script>alert("程序错误");window.location.href="400.html";</script>').end();
                        }else{
                            if(data.length==0){
                                res.send('<script>alert("添加失败");window.location.href="400.html";</script>').end();
                            }
                            else{
                                res.send('<script>alert("添加成功");window.location.href="forumcomment.html";</script>').end();
                            }
                        }
                    });

                }
            });
      }

    });




    //查询
    router.use('/Addforumcomment.html',function (req,res,next) {
        db.query('SELECT * FROM forumbinfo',function (err,data) {
            if(err){
                res.send('<script>alert("程序错误");window.location.href="400.html";</script>')
            }else{
                if(data.length==0) {
                    res.send('<script>alert("暂无您所需的数据");window.location.href="500.html";</script>');
                }
                else{
                    req.forumB=data;
                }
            }

        });
        
        db.query('select *from userinfo',function (err,data) {
            if(err){
                res.send('<script>alert("程序错误");window.location.href="400.html";</script>')
            }else{
                if(data.length==0) {
                    res.send('<script>alert("暂无您所需的数据");window.location.href="500.html";</script>');
                }
                else{
                    console.log(req.forumB);
                    res.render('forum/Addforumcomment.ejs',{formB:req.forumB,user:data});
                }
            }
        });

    });
    return router;
}
